package com.leadership.api.job;

import java.util.Date;

import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.leadership.api.service.NotificationService;

/**
 * 
 * @author dan
 *
 */
@Component("notificationJob")
public class NotificationJob {
    
    private static final Logger logger = LoggerFactory.getLogger(NotificationJob.class);
    
    @Autowired
    private NotificationService notificationService;
    
    /**
     * 每5分钟执行
     */
    @Scheduled(cron = "0 5/5 * * * ?")
    public void scanSendNotification() {
        
        try {
            
            logger.info("#scanSendNotification() - start at {}", new Date());
            
            notificationService.sendNotificationMail();
            
            logger.info("#scanSendNotification() - finish at {}", new Date());
            
        } catch (Exception e) {
            logger.error("#scanSendNotification() - occur exception=[{}]", e);
        }
        
    }
    
    /**
     * 每天23：30
     */
    @Scheduled(cron = "0 30 23 * * ?")
    public void scanCreateMonthlyNotification() {
        
        try {
            
            logger.info("#scanCreateMonthlyNotification() - start at {}", new Date());
            
            Date today = new Date();
            Date day = DateUtils.addDays(today, 1);
            notificationService.createMonthlyNotification(day);
            
            logger.info("#scanCreateMonthlyNotification() - finish at {}", new Date());
            
        } catch (Exception e) {
            logger.error("#scanCreateMonthlyNotification() - occur exception=[{}]", e);
        }
        
    }

}
